extends TextureRect

var data: ClueData
#引用主场景的InfoPanel和对话系统
@onready var info_label = get_tree().current_scene.find_child("InfoPanel").get_child(0)
@onready var dialogue_ui = get_tree().current_scene.find_child("DialogueLayer", true, false)
@onready var timer = $Timer
var is_triggered: bool = false

func setup(_data: ClueData):
	data = _data
	texture = data.icon

func _ready():
	# 连接鼠标信号
	mouse_entered.connect(_start_timer)
	mouse_exited.connect(_reset_timer)
	timer.timeout.connect(_show_info)

func _start_timer():
	timer.start() # 开始倒计时
func _reset_timer():
	timer.stop() # 移开鼠标，打断倒计时
	info_label.get_parent().hide() # 隐藏面板



#点击触发特殊对话
func _gui_input(event):
	if event is InputEventMouseButton and event.pressed and event.button_index == MOUSE_BUTTON_LEFT:
		
		if Global.can_interact and data.special_dialogue.size() > 0 and not is_triggered:
			
			#播放对话
			dialogue_ui.start_dialogue("Memorica", data.special_dialogue)#假设说话人是主角
			
			is_triggered = true
			
			accept_event()

func _show_info():
	if data:
		var name_text = ""
		
		#如果有特殊对话，就把名字标黄
		if data.special_dialogue.size() > 0:
			name_text = "[color=yellow][b]%s[/b][/color]" % data.name
		else:
			#普通物品还是原来的白色加粗
			name_text = "[b]%s[/b]" % data.name
			
		#拼接最终文本
		info_label.text = "%s\n%s" % [name_text, data.description]
		
		info_label.get_parent().show()
